#pragma once

#include "SDL/SDL.h"
#include "srect.h"
#include "graphics.h"
#include "templatearray.h"

struct ImageClipStrip
{
	/** which image to draw clips from */
	SDL_Surface * image;
	/** where in the image to draw clips from */
	TemplateArray<SRect> clips;
	void set(SDL_Surface * a_image, const TemplateArray<SRect> & a_clips)
	{
		image=a_image;
		clips.copy(a_clips);
	}
	inline void copy(const ImageClipStrip & b){set(b.image, b.clips);}
	ImageClipStrip():image(0){}
	void draw(SDL_Surface * a_dest, const SPoint & a_position)
	{
		SPoint p(a_position);
		for(int i = 0; i < clips.size(); ++i)
		{
			VSDL_drawSurfaceToSurface(a_dest, p, image, &(clips.get(i)), 0, 1);
			p.addX(clips.get(i).getWidth());
		}
	}
	void draw(SDL_Surface * a_dest, const SPoint & a_position, 
						 const int & a_flip, const int & a_pixelSize)
	{
		SPoint p(a_position);
		for(int i = 0; i < clips.size(); ++i)
		{
			VSDL_drawSurfaceToSurface(a_dest, a_position, image, &clips.get(i), a_flip, a_pixelSize);
			p.addX(((a_flip&VSDL_FLIP_X)?-1:1)*clips.get(i).getWidth());
		}
	}
	SPoint getDimension()
	{
		SPoint dim;
		if(clips.size() > 0)
		{
			SRect * r = &clips.get(0);
			dim.set(r->getDimension());
//			SPoint p(r->getWidth(), 0);
			for(int i = 1; i < clips.size(); ++i)
			{
				dim.addX(clips.get(i).getWidth());
//				r = &clips.get(i);
//				area.add(r->moved(p));
//				p.addX(r->getWidth());
			}
		}
		return dim;
	}

	void createStringStrip(PixelFont * a_font, const char * a_text)
	{
		image = a_font->getImageSource();
		int length = strlen(a_text);
		clips.allocateToSize(length);
		for(int letter = 0; letter < length; ++letter)
		{
			int index = (unsigned char)a_text[letter];
			//int dist = index * a_tile->w;
			//SRect src(dist % a_source->w, 
			//	(dist / a_source->w) * a_tile->h, 
			//	a_tile->w, a_tile->h);
			//clips.get(letter).set(src);
			clips.get(letter).set(a_font->getImageSourceOf(index));
		}
	}
	void createStringStrip(const char * a_text)
	{
		return createStringStrip(VSDL_getDefaultFont(), a_text);
	}
	ImageClipStrip(const char * a_text):image(0)
	{
		createStringStrip(a_text);
	}
};